package com.newhero.app.report.web.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.newhero.product.common.controller.BaseController;
import com.newhero.product.common.mybatis.mapper.EntityWrapper;
import com.newhero.product.common.util.StringUtils;
import com.newhero.product.common.vo.Json;
import com.newhero.product.common.mybatis.plugins.Page;
import com.newhero.app.report.persistence.entity.ReportResponse;
import com.newhero.app.report.service.IReportResponseService;
import com.newhero.product.util.ExportUtil;

 /**
 * @ClassName: ReportResponseController
 * @Description: 访问控制类
 * @author zlf
 * @date 2018-09-07
 */
@Controller
@RequestMapping("/report/reportResponse")
public class ReportResponseController extends BaseController {
    
    @Autowired 
    private IReportResponseService reportResponseServiceImpl;
    																	
    /**
     * 列表页面
     * @return
     */    
	@RequiresPermissions("/report/reportResponse/reportResponseList") 
	@RequestMapping(value = "/reportResponseList", method ={RequestMethod.GET,RequestMethod.POST })
	public String reportResponseList(
			@RequestParam(value = "page", defaultValue = "1", required = true) int page,// 页号
			@RequestParam(value = "rows", defaultValue = "" + _ROWS_PER_PAGE, required = true) int rows,// 每页显示的记录条数
			@RequestParam(value = "orderField", defaultValue = "response_id", required = true) String orderField,
			@RequestParam(value = "isAsc", defaultValue = "0", required = true) int isAsc,//1-代表true;0-代表false
			ReportResponse reportResponse,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		Page<ReportResponse> p = new Page<ReportResponse>();
		p.setCurrent(page);
		p.setSize(rows);
		p.setOrderByField(orderField);
		if(isAsc==0){
			p.setAsc(false);
		}else{
			p.setAsc(true);
		}
		
		EntityWrapper<ReportResponse> entityWrapper = new EntityWrapper<ReportResponse>();
		
			
    	if(StringUtils.isNotBlank(reportResponse.getRUid())){
			entityWrapper.like("r_uid", reportResponse.getRUid());
		}
		model.addAttribute("rUid", reportResponse.getRUid());
		
			
    	if(StringUtils.isNotBlank(reportResponse.getRUname())){
			entityWrapper.like("r_uname", reportResponse.getRUname());
		}
		model.addAttribute("rUname", reportResponse.getRUname());
		
			
    	if(StringUtils.isNotBlank(reportResponse.getRContent())){
			entityWrapper.like("r_content", reportResponse.getRContent());
		}
		model.addAttribute("rContent", reportResponse.getRContent());
		
			
    	if(StringUtils.isNotBlank(reportResponse.getRDate())){
			entityWrapper.like("r_date", reportResponse.getRDate());
		}
		model.addAttribute("rDate", reportResponse.getRDate());
		
	
		p = reportResponseServiceImpl.selectPage(p, entityWrapper);
		model.addAttribute("data", p);
		model.addAttribute("orderField", orderField);
		model.addAttribute("isAsc", isAsc);
		
		return "/report/reportResponse/reportResponseList";
	}
	
    /**
     * 表单页面
     * @return
     */
	@RequestMapping(value = "/reportResponseForm", method = RequestMethod.GET)
	public String reportResponseForm(String responseId, HttpServletRequest request) {
		if (responseId != null && !"".equals(responseId)) {
			request.setAttribute("responseId", responseId);
			request.setAttribute("formData",reportResponseServiceImpl.selectById(responseId));
		} else {
			request.setAttribute("responseId", "");
			request.setAttribute("formData", new HashMap<String, Object>());
		}
		return "/report/reportResponse/reportResponseForm";
	}
    
    /**
     * 表单保存
     * @return
     */
	@RequestMapping(value = "/reportResponseSave", method = RequestMethod.POST)
	public String reportResponseSave(
			String responseId, ReportResponse reportResponse, 
			HttpServletRequest request,HttpServletResponse response, ModelMap model) {
		try {
			if (!StringUtils.isBlank(responseId)) {
				model.addAttribute("flag", "数据修改成功！");
				reportResponseServiceImpl.updateById(reportResponse);
			} else {
				model.addAttribute("flag", "数据添加成功！");
				reportResponseServiceImpl.insert(reportResponse);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		return "redirect:reportResponseList";
	}

    /**
     * 删除
     * @param ids
     * @return
     */
	@RequestMapping(value = "/reportResponseDelete", method = { RequestMethod.POST, RequestMethod.GET })
	public String reportResponseDelete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String[] idArr = ids.split(",");
		reportResponseServiceImpl.deleteBatchIds(Arrays.asList(idArr));
		model.addAttribute("flag", "数据删除成功！");
		return "redirect:reportResponseList";
	}     
    
    
    @ResponseBody
	@RequestMapping(value = "/reportResponseAjax", method = RequestMethod.POST)
	public Json reportResponseAjax(String responseId) {
		ReportResponse reportResponse = reportResponseServiceImpl.selectById(responseId);
		Json json = new Json();
		json.setSuccess(true);
		json.setObj(reportResponse);
		return json;
	}
	
	@RequestMapping(value = "/reportResponseExportExcel", method = { RequestMethod.GET, RequestMethod.POST })
	public void reportResponseExportExcel(
			ReportResponse reportResponse,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		
		EntityWrapper<ReportResponse> entityWrapper = new EntityWrapper<ReportResponse>();
		
	    if(StringUtils.isNotBlank(reportResponse.getResponseId())){
			entityWrapper.like("response_id", reportResponse.getResponseId());
		}
		model.addAttribute("responseId", reportResponse.getResponseId());
							
		List<ReportResponse> resultList = reportResponseServiceImpl.selectList(entityWrapper);
		
		ExportUtil.export(request, response, resultList,"导出Excel","report_response", 0, 2, 0, 16);
	}
}
